import { Injectable } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { Observable } from 'rxjs';
import { ApiPagedData } from 'src/app/project/model/common/api-paged-data';
import { ConfigurationInfoDTO } from 'src/app/project/model/icm/icm-global/configuration/configuration-info.dto';
import { ApiSimpleData } from 'src/app/project/model/common/api-simple-data';

@Injectable({
  providedIn: 'root',
})
export class ConfigurationService {
  /**
   * API的入口URL
   */
  private static URL = '/api/icm-system-configuration';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询全局参数配置
   * @param option 分页参数
   * @param name 全局参数名称
   */
  findOnePage(option: QueryOptions, name?: string): Observable<ApiPagedData<ConfigurationInfoDTO>> {
    const params = {};
    Object.assign(params, option, name ? { name } : {});
    return this.http.get<ApiPagedData<ConfigurationInfoDTO>>(ConfigurationService.URL, params);
  }

  /**
   * 新增全局参数配置
   */
  create(dictionaryInfo: ConfigurationInfoDTO): Observable<ConfigurationInfoDTO> {
    return this.http.post<ConfigurationInfoDTO>(ConfigurationService.URL, dictionaryInfo);
  }

  /**
   * 提交全局参数配置修改数据
   * @param id 全局参数配置Id
   * @param editInfo 全局参数配置信息
   */
  complete(id: string, editInfo: ConfigurationInfoDTO): Observable<ConfigurationInfoDTO> {
    return this.http.put<ConfigurationInfoDTO>(`${ConfigurationService.URL}/${id}`, editInfo);
  }

  /**
   * 通过id查找系统全局配置参数信息
   * @param id 全局参数配置Id
   */
  findById(id: string): Observable<ConfigurationInfoDTO> {
    return this.http.get<ConfigurationInfoDTO>(`${ConfigurationService.URL}/${id}`);
  }

  /**
   * 删除全局参数配置
   * @param id 全局参数配置id
   */
  delete(id: string): Observable<any> {
    return this.http.delete(`${ConfigurationService.URL}/${id}`);
  }

  /**
   * 查找所有全局参数配置
   */
  findAll(): Observable<Array<ConfigurationInfoDTO>> {
    return this.http.get<Array<ConfigurationInfoDTO>>(`${ConfigurationService.URL}/all`);
  }

  /**
   * 根据全局参数配置id，检查全局参数配置name是否已存在
   * @param name 全局参数配置
   */
  checkoutNameIsExist(name: string): Observable<ApiSimpleData<boolean>> {
    const params = {};
    Object.assign(params, name ? { name } : {});
    return this.http.get<ApiSimpleData<boolean>>(`${ConfigurationService.URL}/only`, params);
  }
}
